package com.plian.system.controller.pf;

import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.entity.pf.LeadershipPayRecord;
import com.plian.system.entity.pf.LeadershipPayRecordDetail;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pf.ILeadershipPayRecordDeclareService;
import com.plian.system.service.pf.ILeadershipPayRecordService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pf.LeadershipPayRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/3/27 10:39 AM
 * @Param
 * @return
 * @Version: 1.0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("api/leadershippay")
@Api(value = "LeadershipPayRecordController", tags = "领导薪酬发放备案 控制器")
public class LeadershipPayRecordController extends BaseController {

    private final ILeadershipPayRecordService leadershipPayRecordService;

    private final ILeadershipPayRecordDeclareService recordDeclareService;

    private final FormWorkflowService formWorkflowService;

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入 leadershipPayRecord")
    public R<LeadershipPayRecordVO> detail(@RequestBody LeadershipPayRecord leadershipPayRecord) {
       return R.data(leadershipPayRecordService.getDetailObj(leadershipPayRecord));
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<LeadershipPayRecordVO> list(@RequestBody Query query) {
        return leadershipPayRecordService.list(query);
    }

    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "新增或修改", notes = "传入 leadershipPayRecord")
    @LogOperation("领导薪酬发放备案-提交")
    public R<LeadershipPayRecord> submit(@Valid @RequestBody LeadershipPayRecord leadershipPayRecord) {
        boolean result = leadershipPayRecordService.submit(leadershipPayRecord);
        HashMap<String, Object> processVariable = new HashMap<>();
        formWorkflowService.startProcessAndSimulate(String.valueOf(leadershipPayRecord.getId()),leadershipPayRecord.getFormType(),processVariable);

        return result ? R.data(leadershipPayRecord) : new R(CommonCode.FAIL);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入 ids")
    @LogOperation("领导薪酬发放备案-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return leadershipPayRecordService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @GetMapping("/getIndex")
    @ApiOperation(value = "获取备案申明指标", notes = "")
    public R<List<AppraisalIndex>> getIndex() {
        return R.data(recordDeclareService.getIndex());
    }

    @PostMapping(value = "/uploadFile",produces="application/json;charset=UTF-8")
    @ApiOperation(value = "导入", notes = "传入导入文件")
    @LogOperation("领导薪酬发放备案-导入")
    public R<List<LeadershipPayRecordDetail>> uploadFile(@RequestParam("file") MultipartFile multipartFile){
        return R.data(leadershipPayRecordService.uploadFile(multipartFile));
    }

    @PostMapping(value = "/toExcel")
    @ApiOperation(value = "导出", notes = "传入对象")
    public void toExcel(@RequestBody LeadershipPayRecordVO leadershipPayRecordVO,HttpServletResponse response) throws Exception {
        leadershipPayRecordService.toExcel(leadershipPayRecordVO,response);
    }

}
